package views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import dialog.CenterDialog;
import greendao.UserDao;
import greendao.entity.UnitInformation;
import greendao.entity.User;

/**
 * Created by Ren on 2017/8/25.
 */

public class ZdfzrView extends RelativeLayout implements View.OnClickListener {

    private AutoCompleteTextView auto_fzr;
    private EditText et_dh;
    private ImageView iv_add, iv_delect;
    private User mSelectUser;
    //在同级ZdfzrView中是否为第一个
    private boolean isFirastView;
    private CenterDialog mCenterDialog;

    public ZdfzrView(Context context, boolean isFirastView) {
        super(context);
        this.isFirastView = isFirastView;
        initView();
        initDate();
        initListen();
    }



    public ZdfzrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initDate();
        initListen();
    }

    public ZdfzrView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initDate();
        initListen();
    }

    private void initView() {
        inflate(getContext(), R.layout.zdfzrview, this);

        auto_fzr = (AutoCompleteTextView) findViewById(R.id.auto_fzr);
        et_dh = (EditText) findViewById(R.id.et_dh);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        iv_delect = (ImageView) findViewById(R.id.iv_delect);

    }


    private void initDate() {
        if (isFirastView) {
            iv_add.setVisibility(VISIBLE);
            iv_delect.setVisibility(GONE);
            iv_add.setOnClickListener(this);
        } else {
            iv_add.setVisibility(GONE);
            iv_delect.setVisibility(VISIBLE);
            iv_delect.setOnClickListener(this);
        }

        //初始化AutoCompleteTextView
        auto_fzr.setAdapter(new ArrayAdapter<User>(getContext(), R.layout.dropdown_item, DictionariesUtil.UnitUser));

    }

    public  void setDate(User user){
        if(user!=null){
            auto_fzr.setText(user.getLjyyxWorkerName());
            et_dh.setText(user.getLjyyxWorkerTel());
        }
    }

    private void initListen() {
        //使用自动补全功能选择的本单位人员，填充电话号码
        auto_fzr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSelectUser = (User) auto_fzr.getAdapter().getItem(position);
                et_dh.setText(mSelectUser.getLjyyxWorkerTel());
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                ((ViewGroup) getParent()).addView(new ZdfzrView(getContext(), false));
                break;
            case R.id.iv_delect:
                //若该View中存在数据，显示删除提示框
                if (!auto_fzr.getText().toString().equals("") || !et_dh.getText().toString().equals("")) {
                    mCenterDialog = new CenterDialog(getContext(), new CenterDialog.CenterClick() {
                        @Override
                        public void confirmListener() {
                            ((ViewGroup) getParent()).removeView(ZdfzrView.this);
                            mCenterDialog.dismiss();
                        }

                        @Override
                        public void cancelListener() {
                            mCenterDialog.dismiss();
                        }
                    });
                    mCenterDialog.setMessage("该记录存在负责人信息，确定删除？");
                    mCenterDialog.show();
                } else {
                    ((ViewGroup) getParent()).removeView(ZdfzrView.this);
                }
                break;

        }
    }

    public User getSelectUser() {

        if (mSelectUser == null && (!auto_fzr.getText().toString().equals("") || !et_dh.getText().toString().equals(""))) {
//            mCenterDialog=new CenterDialog(getContext(), new CenterDialog.CenterClick() {
//                @Override
//                public void confirmListener() {
            mSelectUser = new User();
            mSelectUser.setLjyyxWorkerName(auto_fzr.getText().toString());
            mSelectUser.setLjyyxWorkerTel(et_dh.getText().toString());
//                    mCenterDialog.dismiss();
//                }

//                @Override
//                public void cancelListener() {
//                    mCenterDialog.dismiss();
//                }
//            });
//            mCenterDialog.setMessage("您设置的负责人:"+auto_fzr.getText().toString()+"非本单位工作人员，确定吗?");
//            mCenterDialog.show();
            return mSelectUser;
        } else {
            return mSelectUser;
        }
    }
}
